package com.zlsy.springbootdemo.encryption.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;

/**
 * @author zhouliang
 * @date 2020/12/17 11:10
 */
@Slf4j
public class DecryptUtil {
  public static String decrypt(String content, String requestCharSet, String encryKey) {

    if (StringUtils.isNotBlank(encryKey)) {
      // 解密
      try {
        byte[] key = Base64.decodeBase64(encryKey.getBytes(requestCharSet));
        // content Base64解码
        byte[] contentBytes = Base64.decodeBase64(content.getBytes(requestCharSet));
        byte[] outputData = AESCoder.decrypt(contentBytes, key);
        content = new String(outputData, requestCharSet);
      } catch (Exception e) {
        log.error(e.getMessage(), e);
      }
    }
    return content;
  }
}
